package com.juyu.miliao.scheduler.message;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.common.constant.OfficialAnswerRabbitMqInfo;
import com.juyu.miliao.common.constant.OfficialQuestionInfoRedisKey;
import com.juyu.miliao.common.constant.OfficialQuestionToAnsweRedisKey;
import com.juyu.miliao.scheduler.domin.OfficialQuestionToAnswerInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * @author xuanqicheng
 * @create 2018-24-09:47
 */
@Component

public class OfficialQuestionToAnswerInfoMessage {

    private final Logger logger = LoggerFactory.getLogger("questionAndAnswerLog");

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 更新官方回答缓存
     *
     * @param message,官方回答实体
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(OfficialAnswerRabbitMqInfo.QUEUE_TO_OFFICIAL_ANSWER_QUEUE),
            exchange = @Exchange(OfficialAnswerRabbitMqInfo.EXCHANGE_TO_OFFICIAL_ANSWER_EXCHANGE),
            key = {OfficialAnswerRabbitMqInfo.ROUTING_KEY_TO_UPDATE_OFFICIAL_ANSWER}))
    public void getUpdateOfficialAnser(String message) {

        try {
            if (!StringUtils.isEmpty(message)) {

                OfficialQuestionToAnswerInfo obj = JSONObject.parseObject(message,OfficialQuestionToAnswerInfo.class);

                Set<String> deletKeySet = new HashSet<>();

                String newOfficialAnswerKey = OfficialQuestionToAnsweRedisKey.OFFICIAL_QUESTION_TO_ANSWER_NEW
                        +obj.getOfficialQuestionId();

                deletKeySet.add(newOfficialAnswerKey);

                logger.info("[更新官方回答缓存]根据官方问题ID:{},获取最新回答缓存key={}", obj.getOfficialQuestionId(), newOfficialAnswerKey);

                String officialAnswerPagerKey = OfficialQuestionToAnsweRedisKey
                        .OFFICIAL_QUESTION_TO_ANSWER_BY_QUESTIONID_AND_PAGEINFO+obj.getOfficialQuestionId()+":*";

                Set<String> officialAnswerPageInfoSet = stringRedisTemplate.keys(officialAnswerPagerKey);

                if (officialAnswerPageInfoSet != null && officialAnswerPageInfoSet.size() > 0) {

                    deletKeySet.addAll(officialAnswerPageInfoSet);

                    logger.info("[更新官方回答缓存]根据官方问题ID:{},获取分页回答缓存key={},总共{}个",obj.getOfficialQuestionId(),
                            officialAnswerPagerKey, officialAnswerPageInfoSet.size());
                }

                stringRedisTemplate.delete(deletKeySet);

            }

        } catch (Exception ex) {
            logger.error("[更新官方回答缓存]错误:" + ex.getMessage());
        }
    }
}
